package com.site.dao.impl;

import com.site.dao.GoodsDao;
import com.site.pojo.Goods;
import com.site.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * FileName: GoodsDaoImpl.java
 * 货物表DaoImpl
 *
 * @author 田尚忠
 * @Date    2021/11/28
 * @version 1.00
 */
public class GoodsDaoImpl implements GoodsDao {

    @Override
    public List<Goods> selectAll() throws SQLException {
        List<Goods> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        String sql = "SELECT `goods_id`, `tid`, `producer` FROM goods";
        ResultSet rs = JDBCUtils.executeQuery(conn, sql);
        while (rs.next()){
            Goods goods = new Goods();
            goods.setGoodsId(rs.getLong("goods_id"));
            goods.setTid(rs.getLong("tid"));
            goods.setProducer(rs.getString("producer"));
            list.add(goods);
        }
        JDBCUtils.close(rs,null,conn);
        return list;
    }

    //按照货物种类查询
    @Override
    public List<Goods> selectBytid(int id) throws SQLException {
        List<Goods> list = new ArrayList<>();
        Connection conn = JDBCUtils.getConnection();
        String sql = "SELECT `goods_id`, `tid`, `producer` FROM goods where tid=?";
        ResultSet rs = JDBCUtils.executeQuery(conn, sql,id);
        while (rs.next()){
            Goods goods = new Goods();
            goods.setGoodsId(rs.getLong("goods_id"));
            goods.setTid(rs.getLong("tid"));
            goods.setProducer(rs.getString("producer"));
            list.add(goods);
        }
        JDBCUtils.close(rs,null,conn);
        return list;
    }

    //按照商品id查询
    @Override
    public Goods selectByGoodsid(int id) throws SQLException {
        Goods goods = new Goods();
        Connection conn = JDBCUtils.getConnection();
        String sql = "SELECT `goods_id`, `tid`, `producer` FROM goods where goods_id=?";
        ResultSet rs = JDBCUtils.executeQuery(conn, sql,id);
        while (rs.next()){
            goods.setGoodsId(rs.getLong("goods_id"));
            goods.setTid(rs.getLong("tid"));
            goods.setProducer(rs.getString("producer"));
        }
        JDBCUtils.close(rs,null,conn);
        return goods;
    }

    @Override
    public int addGodds(Goods goods) {
        String sql = "INSERT INTO `hurricane_house`.`goods` (`goods_id`, `tid`, `producer`) VALUES (?, ?, ?);";
        Object [] params = {goods.getGoodsId(),goods.getTid(),goods.getProducer()};
        return JDBCUtils.executeUpdate(sql, params);
    }

    @Override
    public int addGodds(Connection conn, Goods goods) {
        String sql = "INSERT INTO `hurricane_house`.`goods` (`goods_id`, `tid`, `producer`) VALUES (?, ?, ?);";
        Object [] params = {goods.getGoodsId(),goods.getTid(),goods.getProducer()};
        return JDBCUtils.executeUpdate(conn,sql, params);
    }

    @Override
    public int delElectricWireSizeByid(int id) {
        String sql = "DELETE FROM goods WHERE tid = ?";
        return JDBCUtils.executeUpdate(sql,new Object[]{id});
    }

    @Override
    public int delElectricWireSizeByid(Connection conn, int id) {
        String sql = "DELETE FROM goods WHERE tid = ?";
        return JDBCUtils.executeUpdate(conn,sql,new Object[]{id});
    }

    @Override
    public int delElectricWireSizeByGoodsid(int id) {
        String sql = "DELETE FROM goods WHERE goods_id = ?";
        return JDBCUtils.executeUpdate(sql,new Object[]{id});
    }

    @Override
    public int delElectricWireSizeByGoodsid(Connection conn, int id) {
        String sql = "DELETE FROM goods WHERE goods_id = ?";
        return JDBCUtils.executeUpdate(conn,sql,new Object[]{id});
    }

    @Override
    public int updateGoods(int id,Goods goods) {
        String sql = "UPDATE `hurricane_house`.`goods` SET `tid` = ?, `producer` = ? WHERE `goods_id` = ?;";
        Object [] params = {goods.getTid(),goods.getProducer(),id};
        return JDBCUtils.executeUpdate(sql,params);
    }

    @Override
    public int updateGoods(Connection conn, int id,Goods goods) {
        String sql = "UPDATE `hurricane_house`.`goods` SET `tid` = ?, `producer` = ? WHERE `goods_id` = ?;";
        Object [] params = {goods.getTid(),goods.getProducer(),id};
        return JDBCUtils.executeUpdate(conn,sql,params);
    }
}
